package com.hmdp.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.sql.SQLOutput;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@Component
public class RedisidWorker {
//    1970年1月1日0时0分0秒到2024-1-1-0-0-0的时间戳
    private  static  final  long BEGIN_TIMESTAMP = 1704067200L;
    private  static  final  int COUNT = 32;
    private StringRedisTemplate stringRedisTemplate;
    public RedisidWorker(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate=stringRedisTemplate;
    }

    public   long nextid(String keyPrefix) {
        LocalDateTime localDateTime=LocalDateTime.now();
//        nowSecond是 1970年1月1日0时0分0秒到现在时间的时间戳
        long nowSecond = localDateTime.toEpochSecond(ZoneOffset.UTC);
//        现在到2024-1-1-0-0-0的时间戳
        long timestamp=nowSecond - BEGIN_TIMESTAMP;
//        得到当前日期
        String date =localDateTime.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
//       自增长
        long count= stringRedisTemplate.opsForValue().increment("icr"+keyPrefix+":"+date);
        return  timestamp << COUNT |count;
    }

}
